﻿namespace Wicresoft.Platform.Processes.Mobile
{
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using System.Collections;
    using System.Collections.ObjectModel;
    using Wicresoft.Platform.Processes.Mobile.Web.Services;
    using System;
    using System.Windows;
    using System.ServiceModel.DomainServices.Client;
    using Wicresoft.Platform.Processes.Mobile.Controls;

    /// <summary>
    /// Home page for the application.
    /// </summary>
    public partial class Home : Page
    {
        /// <summary>
        /// Creates a new <see cref="Home"/> instance.
        /// </summary>
        public Home()
        {
            InitializeComponent();

            this.Title = ApplicationStrings.HomePageTitle;


            //this.LayoutRoot.Resources.Add("MyList", new MyList());
            //this.DataContext = new MyData();
        }

        /// <summary>
        /// Executes when the user navigates to this page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void wicresoft_Platform_Processes_MobileDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            MobileProcessContext process = new MobileProcessContext();
            //
            process.ExecuteFlow(
                     new Workflows.Mobile.Wicresoft_Platform_Processes_Mobile()
                     {
                         DeployMethod = deployMethodTextBox.Text,
                         LaunchBy = launchByTextBox.Text,
                         LaunchComment = launchCommentTextBox.Text,
                         LaunchTime = System.DateTime.Now,
                         MobileName = mobileNameTextBox.Text,

                         AssignedTo = launchByTextBox.Text,
                         LastModifiedBy = launchByTextBox.Text,
                         LastModifiedTime = System.DateTime.Now,
                         CurrentState = Workflows.Mobile.ProcessState.Launched.ToString(),
                     },
                      Workflows.Mobile.ProcessState.UnInitialized,
                     ExecuteFlow_Completed,
                     null);



        }

        private void ExecuteFlow_Completed(InvokeOperation<Guid> operation)
        {
            if (!operation.HasError)
                MessageBox.Show("Workflow launched successfully with state=" + operation.Value);
            else
                MessageBox.Show("Fail to launch workflow with exception:" + operation.Error);
        }


        //public CollectionView GetDeployMethods()
        //{
        //    return new string[] { "预装", "WAP/WEB" };
        //}
    }

    //public enum DeployMethod
    //{
    //    预装,
    //    WAP_WEB
    //}

    //public class MyData
    //{
    //    public string MyValue { get; set; }
    //}

    //public class MyList
    //{
    //    public MyList()
    //    {
    //        this.Items = new ObservableCollection<string>();
    //        foreach (var item in new string[] { "预装", "WAP/WEB" })
    //            this.Items.Add(item);
    //    }

    //    public ObservableCollection<string> Items { get; private set; }
    //}
}